<?php

class LSina {
	
	/**
	 * 新浪微博影响力计算公式
	 */
	static private function calculateWeight($friends, $followers, $statuses, $create_time, $verified){
		return round($followers*0.9 + $friends*0.05 + $statuses*0.05);
	}
	
	/**
	 * 获取验证的url
	 */
	static public function getAuthUrl($callbackUrl){
		require_once("/lib/weibooauth.class.php");
		$o = new WeiboOAuth(SINA_WB_AKEY, SINA_WB_SKEY);
		$keys = $o->getRequestToken();
		$aurl = $o->getAuthorizeURL($keys['oauth_token'] ,false , $callbackUrl);
		return array('keys'=>$keys, 'aurl'=>$aurl);
	}

    /**
	 * 获取网络信息
	 */
	static public function connect($user_id, $oauth_token, $oauth_token_secret){
		require_once("/lib/weibooauth.class.php");
		$o = new WeiboOAuth(SINA_WB_AKEY , SINA_WB_SKEY , $oauth_token , $oauth_token_secret);
		$auth_keys = $o->getAccessToken($_REQUEST['oauth_verifier']);
		$c = new WeiboClient(SINA_WB_AKEY, SINA_WB_SKEY, $auth_keys['oauth_token'], $auth_keys['oauth_token_secret']);
		$userInfo = $c->show_user($auth_keys['user_id']);
		
		// 记录同步得来的网络信息
		$sync = Table::Fetch('sync_sina', $user_id);
		if($sync){
			return Table::UpdateCache('sync_sina', $user_id, array(
						'friends' => $userInfo['friends_count'],
						'followers' => $userInfo['followers_count'],
						'statuses' => $userInfo['followers_count'],
						'sina_create_time' => strtotime($userInfo['created_at']),
						'verified' => (int)$userInfo['verified'],
						'weight' => self::calculateWeight($userInfo['friends_count'], $userInfo['followers_count'], $userInfo['followers_count'], strtotime($userInfo['created_at']), (int)$userInfo['verified']),
						'last_sync' => time(),
					));
		} else{
			$table = new Table('sync_sina', array(
							'id' => $user_id,
							'sina_uid' => $auth_keys['user_id'],
							'oauth_token' => $auth_keys['oauth_token'],
							'oauth_token_secret' => $auth_keys['oauth_token_secret'],
							'friends' => $userInfo['friends_count'],
							'followers' => $userInfo['followers_count'],
							'statuses' => $userInfo['statuses_count'],
							'sina_create_time' => strtotime($userInfo['created_at']),
							'verified' => (int)$userInfo['verified'],
							'weight' => self::calculateWeight($userInfo['friends_count'], $userInfo['followers_count'], $userInfo['statuses_count'], strtotime($userInfo['created_at']), (int)$userInfo['verified']),
							'last_sync' => time(),
							'create_time' => time(),
						));
			return $table->insert(array('id','sina_uid','oauth_token','oauth_token_secret','friends','followers','statuses','sina_create_time','verified','weight','last_sync','create_time'));
		}
	}
	
	/**
	 * 发布消息
	 */
	static public function publish($user_id, $msg){
		// 发布信息
		$renrenTmp = Table::Fetch('sync_sina', $user_id);
		require_once("/lib/weibooauth.class.php");
		$c = new WeiboClient(SINA_WB_AKEY, SINA_WB_SKEY, $renrenTmp['oauth_token'], $renrenTmp['oauth_token_secret']);
		$rr = $c->update($msg);
		if($rr['error_code']){
			if(($rr['error_code'] == '401') ||($rr['error_code'] == '402')){
				// 授权已取消或微博不存在，则删除同步记录
				LSync::Disconnect($user_id, "sina");
			}
			return $rr;
		} else{
			return Table::UpdateCache('sync_sina', $user_id, array(
						'friends' => $rr['user']['friends_count'],
						'followers' => $rr['user']['followers_count'],
						'statuses' => $rr['user']['followers_count'],
						'verified' => (int)$rr['user']['verified'],
						'weight' => self::calculateWeight($rr['user']['friends_count'], $rr['user']['followers_count'], $rr['user']['statuses_count'], strtotime($rr['user']['created_at']), (int)$rr['user']['verified']),
						'last_sync' => time(),
					));
		}
	}
}
?>